package com.opennews.openplatform.familyexpenseapi.controller

import com.opennews.openplatform.familyexpenseapi.dto.bank.card.BatchDeleteBankCardDto
import com.opennews.openplatform.familyexpenseapi.dto.bank.card.CreateBankCardDto
import com.opennews.openplatform.familyexpenseapi.dto.bank.card.UpdateBankCardDto
import com.opennews.openplatform.familyexpenseapi.service.BankCardService
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse
import com.opennews.openplatform.myspringbootcore.dto.IdentityDto
import com.opennews.openplatform.myspringbootcore.util.getSuccessResponse
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor
import jakarta.validation.Valid
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/bank-card")
class BankCardController(
    private val bankCardService: BankCardService,
    private val requestIdentityExtractor: RequestIdentityExtractor,
) {
    @GetMapping("/query-all-with-basic-info-by-account-group")
    fun queryAllWithBasicInfoByAccountGroup(): ResponseEntity<ApiResponse<List<Map<String, Any>>>> {
        val result =
            bankCardService.queryAllWithBasicInfoByAccountGroup(requestIdentityExtractor.getClaims().accountGroupId.asString())

        return getSuccessResponse(result)
    }

    @GetMapping("/query-basic-info-by-account-group")
    fun queryBasicInfoByAccountGroup(
        @RequestParam pageSize: Int,
        @RequestParam pageIndex: Int,
    ): ResponseEntity<ApiResponse<List<Map<String, Any>>>> {
        val result = bankCardService.queryBasicInfoByAccountGroup(
            requestIdentityExtractor.getClaims().accountGroupId.asString(),
            pageSize,
            pageIndex,
        )

        return getSuccessResponse(result)
    }

    @GetMapping("/query-bank-card-info")
    fun queryBankCardInfo(@RequestParam id: String): ResponseEntity<ApiResponse<Map<String, Any>>> {
        val accountGroupId = requestIdentityExtractor.getClaims().accountGroupId.asString()
        val result = bankCardService.queryBankCardInfo(accountGroupId, id)

        return getSuccessResponse(result)
    }

    @PostMapping("/add")
    fun add(@Valid @RequestBody data: CreateBankCardDto): ResponseEntity<ApiResponse<Any?>> {
        val accountGroupId = requestIdentityExtractor.getClaims().accountGroupId.asString()
        bankCardService.add(accountGroupId, data)

        return getSuccessResponse()
    }

    @PostMapping("/update")
    fun update(@Valid @RequestBody data: UpdateBankCardDto): ResponseEntity<ApiResponse<Any?>> {
        val accountGroupId = requestIdentityExtractor.getClaims().accountGroupId.asString()
        bankCardService.update(accountGroupId, data)

        return getSuccessResponse()
    }

    @PostMapping("/delete")
    fun delete(@Valid @RequestBody data: IdentityDto): ResponseEntity<ApiResponse<Boolean>> {
        val id = data.id.asString()
        val accountGroupId = requestIdentityExtractor.getClaims().accountGroupId.asString()
        val result = bankCardService.delete(accountGroupId, id)

        return getSuccessResponse(result)
    }

    @PostMapping("/batch-delete")
    fun batchDelete(@Valid @RequestBody data: BatchDeleteBankCardDto): ResponseEntity<ApiResponse<Any?>> {
        val ids = data.ids
        val accountGroupId = requestIdentityExtractor.getClaims().accountGroupId.asString()
        bankCardService.delete(accountGroupId, ids)

        return getSuccessResponse()
    }
}
